import ajax from './fetch'
import {
  setTicket
} from '@/utils/auth'

const _url = (key, env) => {
  if ('default' === env)
    return key
  return `${key}/${env}`
}

export const list = () => ajax.get('list')

export const config = (key, env) => ajax.get(`${key}/${env}`)

export const save = (key, env, content) => ajax.post(_url(key, env), {
  config: content
})

export const remove = (key, env) => ajax.delete(_url(key, env))

export const login = (account, password) => {
  return ajax.post('login', {
    account,
    password
  }).then(json => {
    setTicket(json.ticket)
  })
}
